package thread;
//演示，未使用泛型
class MyBlockingQueue{

    private String[] data = null;

    private int head = 0;

    private int tail = 0;

    private int size = 0;

    public MyBlockingQueue(int capacity) {
        data = new String[capacity];
    }

//    public void put(String elem) throws InterruptedException {
//        synchronized (this) {
//            while (size >= data.length) {
//                // 队列满了. 需要阻塞的
//                // return;
//                this.wait();
//            }
//            data[tail] = elem;
//            tail++;
//            if (tail >= data.length) {
//                tail = 0;
//            }
//
//            // tail = (tail + 1) % data.length;
//
//            size++;
//            this.notify();
//        }
//    }

public void put(String elem) throws InterruptedException {
        synchronized (this) {
            while (size >= data.length) {
                this.wait();
            }
            data[tail] = elem;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }

}

    public String take() throws InterruptedException {
        synchronized (this) {
            while (size == 0) {//用if作为作为wait的判定条件,可能存在提前被唤醒的风险,建议使用while
                this.wait();
//                return null;
            }
            String ret = data[head];
            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;

            this.notify();
            return ret;
        }
    }
}

public class Demo31 {
    public static void main(String[] args) {
        MyBlockingQueue queue = new MyBlockingQueue(1000);

        Thread producer = new Thread(()->{
            int n = 0;
            while(true) {
                try {
                    queue.put(n + "");
                    System.out.println("生产元素:" + n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread consumer = new Thread(()->{
            while(true) {
                String n = null;
                try {
                    n = queue.take();
                    System.out.println("消费元素: " + n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        producer.start();
        consumer.start();

    }
}
